﻿using Common;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace SettingServer
{
    [EnableCors("any")]
    //权限控制中心
    public class BaseController : Controller
    {
        private const string V = "";

        /// <summary>
        ///  所有的访问都经过这里,所有Controller的必须继承这Controller,登陆Controller不用继承
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //var paramNames = filterContext.ActionDescriptor.RouteValues;
            //string id = HttpContext.Request.Cookies["admin"];
            //string admin = OperationRedis.GetValue("login" + id);
            //if (!string.IsNullOrEmpty(admin) && admin !="null")
            //{
            //    if (filterContext.HttpContext.Request.Path == "/")
            //    {
            //        //重定向
            //        filterContext.Result = new RedirectResult("~/skip.html?" + "url=http://127.0.0.1:8089/index.html");
            //    }
            //    else
            //    {
            //        OperationRedis.StringSet("login" + id, admin, DateTime.Now.AddMinutes(30) - DateTime.Now);
            //        return;
            //    }
            //}
            //else
            //{
            //    //filterContext.Result = new RedirectResult("~/skip.html?" + "url=http://127.0.0.1:8089/systm/kongbai.html");
            //    //    JsonResult json = new JsonResult("1234567890");
            //    //    //返回string
            //    //    filterContext.Result = json;
            //}
        }

        /// <summary>
        /// Session 存值 默认30分钟，使用请记得
        /// </summary>
        /// <param name="Key">键</param>
        /// <param name="val">值</param>
        /// <param name="Minute">存储时间，分钟</param>
        /// <returns></returns>
        public bool SetSession(string Key, string val, int Minute = 30)
        {
            return OperationRedis.StringSet(Key + HttpContext.Session.Id, val, DateTime.Now.AddMinutes(Minute) - DateTime.Now);
        }

        /// <summary>
        /// Session 取值
        /// </summary>
        /// <param name="Key">键</param>
        /// <returns></returns>
        public string GetSession(string Key)
        {
            return OperationRedis.GetValue(Key + HttpContext.Session.Id);
        }


        /// <summary>
        /// C#反射遍历对象属性
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="model">对象</param>
        public static void ForeachClassProperties<T>(T model)
        {
            Type t = model.GetType();
            PropertyInfo[] PropertyList = t.GetProperties();
            Dictionary<string, object> objDic = new Dictionary<string, object>();
            foreach (PropertyInfo item in PropertyList)
            {
                objDic.Add(item.Name, item.GetValue(model, null));
                //string name = item.Name;
                //object value = item.GetValue(model, null);
            }
        }
    }
}
